package com.modelclient.baselib.http;

import android.text.TextUtils;

import com.modelclient.baselib.config.Config;
import com.modelclient.baselib.util.SPUtils;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * 作者：Hxm
 * 创建时间：2017/7/13 17:06
 * 功能说明：
 */
public class GetCookiesInterceptor implements Interceptor{


    @Override
    public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());
        String setCookie = originalResponse.headers().get("Set-Cookie");
        if(!TextUtils.isEmpty(setCookie)){
            String session = setCookie.substring(setCookie.indexOf("PHPSESSID="), setCookie.length());
            if (session.contains(";")) {
                session = session.substring(0, session.indexOf(";") + 1);
                SPUtils.getInstance().put(Config.SESSION_ID,session);
            }
        }
        return originalResponse;
    }
}
